home *** CD-ROM | disk | FTP | other *** search
-
- #ifndef _HEAPS_
- #define _HEAPS_
-
- /********************************************************************
-
- LIBRARY: Heaps
-
- PURPOSE: To create a simple heap structure allowing
- blocks to created, deleted and iterated over.
-
- AUTHORS: Bill Hubauer (BH)
-
-
- HISTORY:
-
- WHEN WHO WHAT
- ---- --- ----
- 12/27/94 BH Created and debugged
-
-
-
-
- *******************************************************************/
-
-
- typedef void* Heap;
-
-
- class SHeap {
- public:
-
- static Heap NewHeap(Ptr heapArea,Size theSize);
- static void DeleteHeap(Heap h);
-
- static Ptr GetBlock(Heap h,Size sizeNeeded);
- static OSErr FreeBlock(Heap h,Ptr block);
-
- static Size LargestFreeBlock(Heap h);
-
- static Ptr FirstBlock(Heap h);
- static Ptr NextBlock(Ptr block);
-
- static Size BlockSize(Ptr block);
-
- static void DumpHeap(Heap h,FSSpecPtr fs);
- static Size UsedSize(Heap h); // returns total logical size
- // of all blocks allocated.
- static Size HeapSizeForBlock(Size sizeNeeded); // returns the smallest heap store that will let you
- // allocate a block of this size
- };
-
-
-
- /************
- DOCUMENTATION:
-
- This library allows you to create heaps of blocks in a fixed
- memory area.
-
- To create a new heap use the "NewHeap" function
-
- Heap NewHeap(Ptr heapArea,Size theSize);
-
- heapArea: a pointer to a preallocated hunk of memory
- of at least "theSize" size
-
- theSize: Size of memory pointed to by heapArea
-
- When you are finished with a heap you should call the DeleteHeap function.
- Be aware that all blocks in the heap should be considered invalid once
- this all is made. This call does not deallocate the "heapArea" memory that
- was supplied to the NewHeap call.
-
-
- To allocate a block within the heap, call the "GetBlock" function
-
- Ptr GetBlock(Heap h,Size sizeNeeded);
-
- h: This is the result of the NewHeap call
- sizeNeeded: size of block you need
-
- If the call completes successfully you will receive a pointer to
- the block within the heap. Use the FreeBlock function when you
- are finished with the block. If there is insuffiecient contiguous
- free space in the heap, NULL will be returned.
-
- To deallocate a block allocated using the GetBlock call, use the FreeBlock function.
-
- OSErr FreeBlock(Heap h,Ptr block);
-
- h: heap that owns the block
- block: block that was allocated using the GetBlock call.
-
- results:
- -50: block is not valid
-
- To iterate over all allocated blocks, use the FirstBlock and NextBlock functions.
-
- Ptr FirstBlock(Heap h);
-
- h: heap in which you wish the first block.
-
- returns: ptr to first block in heap or NULL if heap is empty
-
-
- Ptr NextBlock(Ptr block);
-
- block: block for which you wish the next block.
-
- returns: ptr to next block after block or NULL if block supplied is last.
-
- NOTE ABOUT ORDER: Currently the order of blocks returned is undefined. It is NOT
- in the order of allocation.
-
-
-
- To determine the logical size of a block, use the BlockSize function.
-
- To determine the largest size of a block that can be allocated using GetBlock,
- use the LargetFreeBlock function.
- **************/
-
-
- #endif // _HEAPS_